﻿#if FRAMEWORK_35
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using VisualEditor.Objects;

namespace VisualEditor.IO
{
    public class AthenaXMLReader : IModelReader
    {

        private static AthenaXMLReader _oInstance = null;
        public static AthenaXMLReader Instance
        {
            get
            {
                if (_oInstance == null) _oInstance = new AthenaXMLReader();
                return _oInstance;
            }
        }
        private AthenaXMLReader()
        {
        }

        #region IModelReader Members

        public Model Read(string sModel)
        {
            XmlDocument oDoc = new XmlDocument();
            oDoc.LoadXml(sModel);

            XmlElement oRoot = oDoc.DocumentElement;

            if (oRoot.Name != "athena")
                throw new Exception("Did not find root element, this is not a valid Athena XML file");

            if (oRoot.GetAttribute("version") != "1.0")
                throw new Exception("Currently the Athena reader only reads version 1.0. Please convert the file first.");

            XmlElement oListOfDrawables = (XmlElement)oRoot.GetElementsByTagName("listOfDrawables")[0];
            XmlNodeList oList = oListOfDrawables.GetElementsByTagName("drawable");

            Model oModel = new Model();

            for (int i = 0; i < oList.Count; i++)
            {
                XmlElement oDrawableXml = (XmlElement)oList[i];

                // read the element
                IDrawable oDrawable = null;

                string sType = oDrawableXml.GetAttribute("type");
                switch (sType)
                {
                    case "protein":
                        oDrawable = new SpeciesGlyph();
                        break;
                    case "NA":
                    default:
                        continue;
                }

                oDrawable.ReadFromXML(oDrawableXml);


                // add drawable to the list
                if (oDrawable != null)
                    oModel.AddToModel(oDrawable, false, false, false);
                    //oModel.Drawables.Add(oDrawable);
            }

            return oModel;

        }

        #endregion
    }
}
#endif